package com.ruoyi.project.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.project.system.domain.SysDynamicFile;
import com.ruoyi.project.system.domain.SysMoment;
import com.ruoyi.project.system.domain.SysMomentFile;
import com.ruoyi.project.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysDynamic;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 动态Controller
 *
 * @author jjp
 * @date 2020-04-16
 */
@RestController
@Api(description = "User动态")
@RequestMapping("/system/dynamic")
public class SysDynamicController extends BaseController {
    @Autowired
    private ISysDynamicService sysDynamicService;

    @Autowired
    private ISysDynamicFileService sysDynamicFileService;

    @Autowired
    private ISysDynamicCommentService sysDynamicCommentService;

    @Autowired
    private ISysUserPariseService sysUserPariseService;

    /**
     * 查询动态列表
     */
    @PreAuthorize("@ss.hasPermi('system:dynamic:list')")
    @ApiOperation("查询动态列表")
    @GetMapping("/list")
    public TableDataInfo list(SysDynamic sysDynamic) {
        startPage();
        List<SysDynamic> list = sysDynamicService.selectSysDynamicList(sysDynamic);
        return getDataTable(list);
    }


    /**
     * 获取动态详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:dynamic:query')")
    @GetMapping(value = "/getInfo/{dynamicId}")
    public AjaxResult getInfo(@PathVariable("dynamicId") Long dynamicId) {
        SysDynamic sysDynamic = sysDynamicService.selectSysDynamicById(dynamicId);
        List<SysDynamicFile> sysDynamicFiles = sysDynamicFileService.selectSysDynamicFileByDynamicId(dynamicId);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < sysDynamicFiles.size(); i++) {
            String url = sysDynamicFiles.get(i).getUrl();
            list.add(url);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("sysDynamic", sysDynamic);
        map.put("list", list);
        return AjaxResult.success(map);
    }

    /**
     * 新增动态
     */
    @PreAuthorize("@ss.hasPermi('system:dynamic:add')")
    @Log(title = "动态", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDynamic sysDynamic) {
        int i = sysDynamicService.insertSysDynamic(sysDynamic);
        List<String> urls = sysDynamic.getUrls();
        Long dynamicId = sysDynamic.getDynamicId();
        for (int j = 0; j < urls.size(); j++) {
            SysDynamicFile sysDynamicFile = new SysDynamicFile();
            String url = urls.get(j);
            sysDynamicFile.setDynamicId(dynamicId);
            sysDynamicFile.setUrl(url);
            sysDynamicFileService.insertSysDynamicFile(sysDynamicFile);
        }
        return toAjax(i);
    }

    /**
     * 修改动态
     */
    @PreAuthorize("@ss.hasPermi('system:dynamic:edit')")
    @Log(title = "动态", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDynamic sysDynamic) {
        return toAjax(sysDynamicService.updateSysDynamic(sysDynamic));
    }

    /**
     * 删除动态
     */
    @PreAuthorize("@ss.hasPermi('system:dynamic:remove')")
    @Log(title = "动态", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dynamicIds}")
    public AjaxResult remove(@PathVariable Long[] dynamicIds) {
        return toAjax(sysDynamicService.deleteSysDynamicByIds(dynamicIds));
    }
}
